package com.xr.miniprogram.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.common.utils.DateUtils;
import com.xr.miniprogram.domain.MiniProgramUser;
import com.xr.miniprogram.domain.dto.MiniProgramUserDTO;
import com.xr.miniprogram.domain.vo.MiniProgramUserVO;
import com.xr.miniprogram.mapper.MiniProgramUserMapper;
import com.xr.miniprogram.service.IMiniProgramUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class IMiniProgramUserServiceImpl extends ServiceImpl<MiniProgramUserMapper, MiniProgramUser> implements IMiniProgramUserService {
    @Resource
    private RestTemplate restTemplate;
    @Value("${wxlogin.code2Session}")
    public String code2SessionUrl;

    @Value("${wxlogin.appId}")
    public String appId;
    @Value("${wxlogin.secret}")
    public String secret;

    @Autowired
    private MiniProgramUserMapper miniProgramUserMapper;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public MiniProgramUser login(MiniProgramUserVO miniProgramUserVO) {
        if (miniProgramUserVO==null || miniProgramUserVO.getCode()==null){
            log.error("Code未正常传递");
            return null;
        }
        //根据code获取openid
        String url = code2SessionUrl+"?appid="+appId+"&secret="+secret+"&js_code="+miniProgramUserVO.getCode()+"&grant_type=authorization_code";
        log.info("发送的请求：{}",url);
        String res = restTemplate.getForObject(url, String.class);
        MiniProgramUserDTO miniProgramUserDTO = JSONObject.parseObject(res, MiniProgramUserDTO.class);
        if (miniProgramUserDTO.getErrcode()==null){
            //根据openid查找用户
            MiniProgramUser currentMiniProgramUser = miniProgramUserMapper.selectMiniProgramUserByOpenId(miniProgramUserDTO.getOpenid());
            if (ObjectUtils.isNull(currentMiniProgramUser)){
                MiniProgramUser miniProgramUser = MiniProgramUser.buildDefault(miniProgramUserDTO.getOpenid());
                if (miniProgramUser!=null) {
                    this.save(miniProgramUser);
                    return miniProgramUser;
                }else{
                    return null;
                }
            }else{
                return currentMiniProgramUser;
            }
        }else{
            //失败
            return null;
        }
    }

    @Override
    public List<MiniProgramUser> selectMiniProgramUserList(MiniProgramUser miniProgramUser) {
        return miniProgramUserMapper.selectMiniProgramUserList(miniProgramUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(MiniProgramUser miniProgramUser) {
        return miniProgramUserMapper.updateMiniProgramUser(miniProgramUser);
    }

    @Override
    public MiniProgramUser selectMiniProgramUserByMiniProgramUserId(Long miniProgramUserId) {
        return miniProgramUserMapper.selectMiniProgramUserByMiniProgramUserId(miniProgramUserId);
    }
}
